package com.gin.no_gay_alliance.callback;

import androidx.annotation.NonNull;

import com.gin.nga.callback.JsonCallback;
import com.gin.nga.exception.NgaException;

import java.io.IOException;

import okhttp3.Call;

/**
 * 请求回调对接
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 14:01
 **/
public class RequestCallback<T> extends JsonCallback<T> {
    @NonNull
    private final Callback<T> callback;

    public RequestCallback(@NonNull Callback<T> callback) {
        this.callback = callback;
    }

    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        super.onFailure(call, e);
        callback.onFailure(call, e);
    }

    /**
     * 执行成功回调
     *
     * @param body body字符串
     */
    @Override
    public void onSuccess(T body) {
        callback.onSuccess(body);
    }

    /**
     * 处理异常
     *
     * @param e 异常
     */
    @Override
    public void handleException(NgaException e) {
        super.handleException(e);
        callback.handleException(e);
    }
}
